﻿using System;
using UnityEngine;

/// <summary>
/// 属性值类，管理单个属性的各种加成值和最终计算。只开放baseValue和levelBonus的设置，其他的只允许加减 不允许设置
/// </summary>
[Serializable]
public class AttributeValue
{
    /// <summary>
    /// 基础值，角色的原始属性值
    /// </summary>
    public float baseValue;

    /// <summary>
    /// 等级加成，随着等级提升获得的属性加成
    /// </summary>
    public float levelBonus;

    /// <summary>
    /// 固定属性加成(来自装备，翅膀，法器，专武等固定值加成)
    /// </summary>
    [SerializeField] public AttributeValeItem equipmentBonus;

    /// <summary>
    /// Buff加成，来自各种状态效果的属性加成,百分比加成，所有百分比形式的属性提升（1.0 = 100%）
    /// </summary>
    [SerializeField] public AttributeValeItem buffBonus;

    /// <summary>
    /// 百分比加成，所有百分比形式的属性提升（1.0 = 100%），(来自装备，翅膀，法器，专武等百分比加成)
    /// </summary>
    [SerializeField] public AttributeValeItem skillBonus;


    /// <summary>
    /// 计算并返回属性的最终值
    /// 计算公式：(基础值 + 装备 + buff + 技能）
    /// </summary>
    public float FinalValue()
    {
        return skillBonus.Add(buffBonus.Add(equipmentBonus.Add(baseValue + levelBonus)));
    }


    public AttributeValue(float baseValue)
    {
        this.baseValue = baseValue;
        this.levelBonus = 0;
        this.equipmentBonus = new AttributeValeItem();
        this.buffBonus = new AttributeValeItem();
        this.skillBonus = new AttributeValeItem();
    }


    public void SetBaseValue(float value)
    {
        baseValue = value;
    }

    public void SetLevelBonus(float value)
    {
        levelBonus = value;
    }

    public void AddEquipmentFixedBonus(float value)
    {
        equipmentBonus.fixedValue += value;
    }

    public void RemoveEquipmentFixedBonus(float value)
    {
        equipmentBonus.fixedValue -= value;
    }

    public void AddEquipmentPercentBonus(float value)
    {
        equipmentBonus.percentageValue += value;
    }

    public void RemoveEquipmentPercentBonus(float value)
    {
        equipmentBonus.percentageValue -= value;
    }

    public void AddBuffFixedBonus(float value)
    {
        buffBonus.fixedValue += value;
    }

    public void RemoveBuffFixedBonus(float value)
    {
        buffBonus.fixedValue -= value;
    }

    public void AddBuffPercentBonus(float value)
    {
        buffBonus.percentageValue += value;
    }

    public void RemoveBuffPercentBonus(float value)
    {
        buffBonus.percentageValue -= value;
    }

    public void AddSkillFixedBonus(float value)
    {
        skillBonus.fixedValue += value;
    }

    public void RemoveSkillFixedBonus(float value)
    {
        skillBonus.fixedValue -= value;
    }

    public void AddSkillPercentBonus(float value)
    {
        skillBonus.percentageValue += value;
    }

    public void RemoveSkillPercentBonus(float value)
    {
        skillBonus.percentageValue -= value;
    }


    [Serializable]
    public class AttributeValeItem
    {
        public float fixedValue;
        public float percentageValue;

        public AttributeValeItem()
        {
            fixedValue = 0;
            percentageValue = 0;
        }

        public float Add(float f)
        {
            return (f + fixedValue) * (1 + percentageValue);
        }
    }
}